# Category=Other

#@ Interface for ISKRA kWh-meter pulse counter
# Author: oil oil@pp.inet.fi

# Note: You have to enabled generic serial ports
#       by editing the serialx parms in mh.ini (or mh.private.ini)
#
# For example:  serial1_port     = COM1
#               serial1_baudrate = 9600
#               serial2_port     = COM2
#
#
# Version history:
# version	date	author	explanation
# v.0.0.50	071126	oil	First commented version
# v.0.0.51	090315	oil	Some modifications and comments
#
#
# Todo list:
# version	date	author	explanation
#
#


my $temp_answer;
 
$serial_in    = new  Serial_Item('Alustus'            , 'init1' , 'serial1');
$serial_in   -> add             ('kW is totaly silent', 'silent');
$serial_in   -> add             ('kW says allways'    , 'sayall');
if ($Startup) {
  set $serial_in 'init1';
  set_dtr $serial_in 1;
  set_rts $serial_in 0;
  print_log "kw_meter: Serial signals lines set" if $Debug{user_code};
#  my $timedelta = 10;
}


$v_serial_in  = new  Voice_Cmd("Power consumption is [silent,saytop,saychance,sayall]");
$kw_cmd       = new  Voice_Cmd("Power consumption in last [minute, 10 minute, hour, day, week, month]");

$kw_meter  = new Generic_Item;
$kw_meter -> {counterI}     = 21584   if not $kw_meter -> {counterI};
$kw_meter -> {counterII}    = 18035   if not $kw_meter -> {counterII};
$kw_meter -> {counter_I}    ;
$kw_meter -> {counter_II}   ;
$kw_meter -> {imp2kwh}      = 1000    if $Reload;
$kw_meter -> {timedelta}    = 10      if $Reload;
$kw_meter -> {lastbillI}    = 20125   if $Reload;
$kw_meter -> {lastbillII}   = 16803   if $Reload;
$kw_meter -> {startI_time}  = '8:00'  if $Reload;
$kw_meter -> {startII_time} = '22:00' if $Reload;
$kw_meter -> {startI_day}   = 30.9    if $Reload;
$kw_meter -> {startII_day}  = 1.5     if $Reload;
$kw_meter -> {isI}          = 1       if $Reload;
$kw_meter -> {power_now}           ;
$kw_meter -> {power_last}          ;
$kw_meter -> {counter_now_minute}    ;
$kw_meter -> {counter_last_minute}   ;
$kw_meter -> {counter_now_10minute}  ;
$kw_meter -> {counter_last_10minute} ;
$kw_meter -> {counter_now_hour}      ;
$kw_meter -> {counter_last_hour}     ;
$kw_meter -> {counter_now_day}       ;
$kw_meter -> {counter_last_day}      ;
$kw_meter -> {counter_now_week}      ;
$kw_meter -> {counter_last_week}     ;
$kw_meter -> {counter_now_month}     ;
$kw_meter -> {counter_last_month}    ;
$kw_meter -> restore_data('counterI', 'counterII', 'counter_I', 'counter_II');    


my $state;
set $serial_in $state if $state = said $v_serial_in;

set $serial_in "Luku alkaapi";

if ($Startup) {
  set_dtr $serial_in 1;      ##############someting wrong here ############
  set_rts $serial_in 0;      ##############someting wrong here ############
  print_log "kw_meter: Serial signals lines set" if $Debug{user_code};
#  my $timedelta = 10;
}

if ('sayall' eq state $v_serial_in or new_hour) {
  my $energyI  =  $kw_meter->{counterI}  - $kw_meter->{lastbillI};
  my $energyII =  $kw_meter->{counterII} - $kw_meter->{lastbillII};
  speak("Energy meter counter 1 is $kw_meter->{counterI} and counter 2 is $kw_meter->{counterII}.");
  speak("Day time energy used since last billing is $energyI kilo watt hours. ");
  speak("Night time energy used since last billing is $energyII kilo watt hours. ");
}


#$kw_meter->{isI} = true  if time $kw_meter->{startI_time};
#$kw_meter->{isI} = false if time $kw_meter->{startII_time};

my $data;
if (0) {
  if (0 < length(my $d=said $serial_in)) {
  if (length($data) > length($d)) {
    my $kw_count = length $data;
    $kw_meter -> {counter_now_minute} += $kw_count;
print "Kw meter dat: " . unpack('H*',$data) . " " . $kw_count . "\n";
########################## 1000 imp / kWh = 1000 imp / 3600 kWs => 1 imp / s = 3600 W 
    $kw_meter->{power_now} = ($kw_count * 3600) / ($kw_meter->{timedelta} * $kw_meter->{imp2kwh});
    logit("./../../data/logs/serial1.$Year_Month_Now.log",   "$kw_count, $kw_meter->{power_now}, $data");
    print_log "kw_meter: Pulses received in $kw_meter->{timedelta} sec: $kw_count, $kw_meter->{power_now}" if $Debug{user_code};
    if ($kw_meter->{isI}) {
      $kw_meter->{counter_I} += $kw_count;
      if ($kw_meter->{counter_I} ge $kw_meter->{imp2kwh}) {
        $kw_meter->{counter_I} -= $kw_meter->{imp2kwh};
        $kw_meter->{counterI} += 1;
      }
    } else {
      $kw_meter->{counter_II} += $kw_count;
      if ($kw_meter->{counter_II} ge $kw_meter->{imp2kwh}) {
        $kw_meter->{counter_II} -= $kw_meter->{imp2kwh};
        $kw_meter->{counterII} += 1;
      }
    }
  } else {
  }
    $data     = $d;
  }
} else {
  if (new_second $kw_meter -> {timedelta}) {
    $kw_meter->{counterI} += $kw_meter->{timedelta}/$kw_meter->{imp2kwh};
    my $data     = said $serial_in;
    my $kw_count = length $data;
    $kw_meter -> {counter_now_minute} += $kw_count;
#    1000 imp / kWh = 1000 imp / 3600 kWs => 1 imp / s = 3600 W 
    my $power    = ($kw_count * 3600) / ($kw_meter->{timedelta} * $kw_meter->{imp2kwh});
    logit("./../../data/logs/serial1.$Year_Month_Now.log",  "$kw_count, $power, $data");
    print_log "kw_meter: Pulses received in $kw_meter->{timedelta} sec: $kw_count, $power" if $Debug{user_code};
  
    #speak(voice  => 'male',
    #        text => "This is an example of a xxx voice"); # if $state = said $test_voice3;
    #print_log state $v_serial_in;
    if ('sayall' eq state $v_serial_in or new_minute) {
      speak("My power consumption is now $power kilo watts.") if $Main::Debug{user_code};
    }
  }
}

if ('sayall' eq state $v_serial_in or new_minute) {
#  speak("My power consumption is now $kw_meter->{power_now} kilo watts.");
}

if (new_minute) {
  $kw_meter -> {counter_last_minute}  = $kw_meter -> {counter_now_minute} ;
  $kw_meter -> {counter_now_minute}   = 0 ;
  $kw_meter -> {counter_now_10minute} += $kw_meter -> {counter_last_minute} ;
}

if (new_minute 10) {
  $kw_meter -> {counter_last_10minute} = $kw_meter -> {counter_now_10minute} ;
  $kw_meter -> {counter_now_10minute}  = 0 ;
  $kw_meter -> {counter_now_hour}     += $kw_meter -> {counter_last_10minute} ;
  $kw_cmd->respond("Ten minutes avarage power consumption is " . $kw_meter -> {counter_last_10minute} * 6 / $kw_meter->{imp2kwh} );
}
  
if (new_hour) {
  $kw_meter -> {counter_last_hour} = $kw_meter -> {counter_now_hour} ;
  $kw_meter -> {counter_now_hour}  = 0 ;
  $kw_meter -> {counter_now_day}   += $kw_meter -> {counter_last_hour} ;
print  "Last hour count : " . $kw_meter -> {counter_last_hour} . "\n";
}

if (my $state = state_now $kw_cmd) {
  $temp_answer = "Last minute count: "    . $kw_meter -> {counter_last_minute}   . " (Wh)\n" if $state eq 'minute';
  $temp_answer = "Last 10 minute count: " . $kw_meter -> {counter_last_10minute} . " (Wh)\n" if $state eq '10 minute';
  $temp_answer = "Last hour count: "      . $kw_meter -> {counter_last_hour}     . " (Wh)\n" if $state eq 'hour';
  $temp_answer = "Last day count: "       . $kw_meter -> {counter_last_day}      . " (Wh)\n" if $state eq 'day';
  $temp_answer = "Last week count: "      . $kw_meter -> {counter_last_week}     . " (Wh)\n" if $state eq 'week';
  $temp_answer = "Last month count: "     . $kw_meter -> {counter_last_month}    . " (Wh)\n" if $state eq 'month';
#  print  "Unknown KW command";
  print $temp_answer;
  $kw_cmd->respond($temp_answer);
}

